package com.baozun.houji.me.fragment

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.adapter.CommonGoodsSelfAdapter
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseFragment
import com.baozhun.mall.common.ext.loadArrayListData
import com.baozhun.mall.common.util.Constants
import com.baozhun.mall.common.widget.EmptyView
import com.baozhun.mall.common.widget.loading.CustomLoadMoreView
import com.baozun.houji.me.R
import com.baozun.houji.me.databinding.FragmentBrowseHistoryBinding
import com.baozun.houji.me.viewmodel.BrowseHistoryViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.chad.library.adapter.base.module.DraggableModule
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * @Title：浏览历史记录Fragment
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/12
 * @Version：1.0.0
 */
class BrowseHistoryFragment : BaseFragment<BrowseHistoryViewModel, FragmentBrowseHistoryBinding>(),
    DraggableModule, OnItemClickListener, OnRefreshListener, OnLoadMoreListener {

    private val mBrowseHistoryListAdapter by lazy { CommonGoodsSelfAdapter(mutableListOf(), false, this) }

    companion object {
        fun newInstance(): BrowseHistoryFragment {
            val args = Bundle()
            val fragment = BrowseHistoryFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBrowseHistoryListAdapter.setEmptyView(
            EmptyView.Builder(mActivity).emptyHintText(com.baozhun.mall.res.R.string.no_browse_history_data_hint)
                .emptyImage(R.drawable.ic_data_empty_colloect)
                .build()
        )
        mDataBinding.baseRefreshLayout.setOnRefreshListener(this)
        mBrowseHistoryListAdapter?.loadMoreModule?.setOnLoadMoreListener(this)
        mBrowseHistoryListAdapter.setOnItemClickListener(this)
        mBrowseHistoryListAdapter.loadMoreModule.loadMoreView = CustomLoadMoreView().apply { setLoadMoreEndText(getString(com.baozhun.mall.res.R.string.browse_history_save_hint)) }
        mDataBinding.rvBrowseList.adapter = mBrowseHistoryListAdapter

    }

    override fun initData() {
        super.initData()
        mDataBinding.viewPageLoad.showPlaceholderView(150)
        mViewModel.getBrowseHistoryList(true)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.mBrowseHistoryList.observe(this) {
            mDataBinding.viewPageLoad.hidePlaceholderView()
            loadArrayListData(it, mBrowseHistoryListAdapter, mDataBinding.baseRefreshLayout)
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        ARouter.getInstance().build(RoutePath.Home.GOODS_DETAIL)
            .withString(Constants.Goods.GOODS_ID, mBrowseHistoryListAdapter.getItem(position).goods_id)
            .withSerializable(Constants.Goods.GOODS_SOURCE, mBrowseHistoryListAdapter.getItem(position).source)
            .navigation()
    }

    override fun useFragmentViewModelStoreOwner(): Boolean {
        return true
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mViewModel.getBrowseHistoryList(true)
    }

    override fun onLoadMore() {
        mViewModel.getBrowseHistoryList(false)
    }


}